#!/usr/bin/env bash
set -e

trap "exit 0" SIGTERM

RETRIES=300 # 5 minutes
until psql "postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}/${POSTGRES_DB}" -c "select 1" > /dev/null 2>&1 || [ $RETRIES -eq 0 ]; do
  echo "Waiting for postgres server, $((RETRIES--)) remaining attempts..."
  sleep 1
done

if [[ $# -gt 0 ]]; then
    exec "$*"
fi